package org.example.managementsystem.controller;

import org.example.managementsystem.model.LeaveApplication;
import org.example.managementsystem.model.LeaveApproval;
import org.example.managementsystem.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/leaves")
@CrossOrigin(origins = "*")
public class LeaveController {

    @Autowired
    private LeaveService leaveService;

    /**
     * 获取所有请假申请
     */
    @GetMapping
    public ResponseEntity<List<LeaveApplication>> getAllLeaveApplications() {
        List<LeaveApplication> applications = leaveService.getAllLeaveApplications();
        return ResponseEntity.ok(applications);
    }

    /**
     * 获取指定排班的请假申请
     */
    @GetMapping("/schedule/{scheduleId}")
    public ResponseEntity<List<LeaveApplication>> getLeaveApplicationsByScheduleId(
            @PathVariable Long scheduleId) {
        List<LeaveApplication> applications = leaveService.getLeaveApplicationsByScheduleId(scheduleId);
        return ResponseEntity.ok(applications);
    }

    /**
     * 获取请假审批详情
     */
    @GetMapping("/approval/{applicationId}")
    public ResponseEntity<?> getLeaveApprovalById(@PathVariable Long applicationId) {
        LeaveApproval approval = leaveService.getLeaveApprovalById(applicationId);
        if (approval == null) {
            Map<String, String> response = new HashMap<>();
            response.put("message", "未找到对应的审批记录");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
        return ResponseEntity.ok(approval);
    }

    /**
     * 提交请假申请
     */
    @PostMapping("/apply")
    public ResponseEntity<?> submitLeaveApplication(@RequestBody LeaveApplication application) {
        try {
            LeaveApplication savedApplication = leaveService.submitLeaveApplication(application);
            return ResponseEntity.status(HttpStatus.CREATED).body(savedApplication);
        } catch (IllegalArgumentException e) {
            Map<String, String> response = new HashMap<>();
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        } catch (Exception e) {
            Map<String, String> response = new HashMap<>();
            response.put("message", "提交请假申请失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 审批请假申请
     */
    @PostMapping("/approve")
    public ResponseEntity<Map<String, Object>> approveLeaveApplication(@RequestBody LeaveApproval approval) {
        Map<String, Object> result = leaveService.approveLeaveApplication(approval);
        boolean success = (boolean) result.get("success");
        return ResponseEntity.status(success ? HttpStatus.OK : HttpStatus.BAD_REQUEST).body(result);
    }

    /**
     * 获取部门月度请假统计
     */
    @GetMapping("/statistics/{year}/{month}/{department}")
    public ResponseEntity<Map<String, Object>> getLeaveStatistics(
            @PathVariable int year,
            @PathVariable int month,
            @PathVariable String department) {
        // 这里可以实现请假统计功能，统计各部门请假情况
        // 目前返回一个空的统计结果
        Map<String, Object> statistics = new HashMap<>();
        statistics.put("year", year);
        statistics.put("month", month);
        statistics.put("department", department);
        statistics.put("totalLeaves", 0);
        statistics.put("approvedLeaves", 0);
        statistics.put("rejectedLeaves", 0);
        
        return ResponseEntity.ok(statistics);
    }
    /**
     * 根据员工工号获取请假申请
     */
    @GetMapping("/employee/{jobNumber}")
    public ResponseEntity<List<LeaveApplication>> getLeaveApplicationsByEmployeeId(
            @PathVariable String jobNumber) {
        List<LeaveApplication> applications = leaveService.getLeaveApplicationsByEmployeeJobNumber(jobNumber);
        return ResponseEntity.ok(applications);
    }
}